<!--
 * @Descripttion: your project
 * @version: 1.0
 * @Author: sunxiantao
 * @Date: 2022-10-19 11:53:29
 * @LastEditors: sunxiantao
 * @LastEditTime: 2022-10-20 17:57:51
-->
<template>
  <section class="class-main">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model.trim="listQuery.userName" placeholder="请输入姓名搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <el-collapse-transition>
      <section v-show="!searchshow" class="main-search separate">
        <section class="search-inner">
          <el-row>
            <el-col :span="8">
              <section class="search-inner-item">
                <div class="item-label">姓名</div>
                <el-input v-model.trim="search.userName" style="width: 90%" class="filter-item" placeholder="请输入姓名搜索" />
              </section>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">结课状态</div>
                <el-radio-group v-model="search.concludeStatus">
                  <el-radio v-for="item in dict['WCZT']" :key="item.id" :label="item.dictValue">{{ item.dictName }}</el-radio>
                </el-radio-group>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">证书状态</div>
                <el-radio-group v-model="search.certificateStatus">
                  <el-radio v-for="item in dict['HQZT']" :key="item.id" :label="item.dictValue">{{ item.dictName }}</el-radio>
                </el-radio-group>
              </div>
            </el-col>
          </el-row>
        </section>
        <section class="search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </section>
      </section>
    </el-collapse-transition>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>专区成绩管理</span>
      </section>
      <section class="main-buttons-btns" />
      <el-button v-if="manualStatus" style="margin-left: 10px" @click="publishedResults">发布成绩</el-button>
      <!-- <el-button style="margin-left: 10px" @click="importResults">导入成绩</el-button> -->
      <!-- <el-button style="margin-left: 10px" @click="exportResults">导出</el-button> -->
      <el-button style="margin-left: 10px" @click="$router.back()">返回</el-button>
    </section>
    <section class="main-table">
      <v-table
        :list="list"
        tag="performanceManagement"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :selection="true"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column label="操作" fixed="right" align="center" min-width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.concludeStatus === 'WCZT_WWC'" type="text" size="mini" @click="editSessionState(scope.row)"
              >修改结课状态</el-button
            >
            <el-button type="text" size="mini" @click="ExamManagement(scope.row)">考试成绩管理</el-button>
            <el-button type="text" size="mini" @click="checkCertificate(scope.row)">查看证书</el-button>
            <el-button type="text" size="mini" @click="remake(scope.row)">备注</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </section>
    <!-- 修改备注 -->
    <el-dialog v-el-drag-dialog title="修改备注" :visible.sync="dialogRemarkVisible" center :close-on-click-modal="false">
      <el-input v-model="updateRemark.remark" type="textarea" :rows="6" placeholder="请输入备注" />
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="dialogRemarkVisible = false">取消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="handleUpdateRemark()">确定</el-button>
      </div>
    </el-dialog>
    <!-- 修改结课状态 -->
    <el-dialog v-el-drag-dialog width="500px" title="修改结课状态" :visible.sync="dialogStatusVisible" center :close-on-click-modal="false">
      <el-radio-group v-model="updateStatus.concludeStatus">
        <el-radio v-for="item in dict['WCZT']" :key="item.id" :label="item.dictValue">{{ item.dictName }}</el-radio>
      </el-radio-group>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="dialogStatusVisible = false">取消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="handleUpdateStatus()">确定</el-button>
      </div>
    </el-dialog>
    <!-- 查看证书 -->
    <el-dialog v-el-drag-dialog :visible.sync="dialogCertificateVisible" title="查看证书" center :close-on-click-modal="false">
      <img width="100%" :src="dialogCertificateUrl" alt="" />
    </el-dialog>
    <!-- 考试成绩管理列表 -->
    <el-dialog v-el-drag-dialog title="考试成绩" :visible.sync="testScoresVisible" center :close-on-click-modal="false" destroy-on-close>
      <section class="main-table">
        <v-table :list="ScoresList" tag="testScoresList" :list-loading="testScoresLoading" :selection="true" />
        <pagination v-show="totals > 0" :total="totals" :page.sync="listQuerys.page" :limit.sync="listQuerys.limit" @pagination="getScoresList" />
      </section>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="testScoresVisible = false">取消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="testScoresVisible = false">确定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { queryPage, getDictByCode, publishScore, updateRemark, updateStatus, concludeGetData, ScoreList } from '../api/index'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import permission from '@/directive/permission/index.js'
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
export default {
  name: 'NewsMain',
  components: { Pagination, VTable },
  directives: { permission, elDragDialog },
  data() {
    return {
      manualStatus: false,
      areaId: '',
      listQuery: {
        page: 1,
        limit: 10,
        userName: ''
      },
      listQuerys: {
        page: 1,
        limit: 10,
        userName: ''
      },
      dict: [],
      searchshow: true,
      search: {
        userName: '',
        concludeStatus: '',
        certificateStatus: ''
      },
      list: [],
      ScoresList: [],
      total: 0,
      totals: 0,
      listLoading: false,
      testScoresLoading: false,
      multipleSelection: [],
      updateRemark: {},
      dialogRemarkVisible: false,
      updateStatus: {},
      dialogStatusVisible: false,
      view: '/API-DOCUMENT/document/file/download?id=',
      dialogCertificateUrl: '', // 查看证书
      dialogCertificateVisible: false,
      testScoresVisible: false,
      userId: ''
    }
  },
  created() {
    if (!this.$route.query.id) {
      this.$message({
        showClose: true,
        message: '参数异常！',
        type: 'warning',
        onClose: () => {
          this.$router.back()
        }
      })
    } else {
      this.areaId = this.$route.query.id
      this.getDict('WCZT')
      this.getDict('HQZT')
      this.getList()
    }
  },
  methods: {
    // 发布成绩
    publishedResults() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        publishScore({ areaId: this.areaId })
          .then((res) => {
            loading.close()
            if (res.data.code === '200') {
              loading.close()
              this.$message({
                type: 'success',
                message: '发布成功!'
              })
              this.getList()
            }
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    // 导入成绩
    importResults() {
      console.log('导入成绩')
    },
    // 导出成绩
    exportResults() {
      console.log('导出成绩')
    },
    /**
     * 修改结课状态
     */
    editSessionState(row) {
      this.updateStatus = row
      this.dialogStatusVisible = true
    },
    handleUpdateStatus() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const params = {
        id: this.updateStatus.id,
        concludeStatus: this.updateStatus.concludeStatus
      }
      return new Promise((resolve, reject) => {
        updateStatus(params)
          .then((res) => {
            loading.close()
            if (res.data.code === '200') {
              loading.close()
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogStatusVisible = false
              this.getList()
            }
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    /**
     * 考试成绩管理
     */
    ExamManagement(row) {
      this.userId = row.userId
      this.testScoresVisible = true
      this.$nextTick(() => {
        this.getScoresList()
      })
    },
    /**
     * 查看证书
     */
    checkCertificate(row) {
      if (!row.certificateImg) {
        this.$message({
          type: 'error',
          message: '您还没获得证书!'
        })
      } else {
        this.dialogCertificateUrl = row.certificateImgUrl
        this.dialogCertificateVisible = true
      }
    },
    /**
     * 修改备注
     */
    remake(row) {
      this.updateRemark = row
      this.dialogRemarkVisible = true
    },
    handleUpdateRemark() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const params = {
        id: this.updateRemark.id,
        remark: this.updateRemark.remark
      }
      return new Promise((resolve, reject) => {
        updateRemark(params)
          .then((res) => {
            loading.close()
            if (res.data.code === '200') {
              loading.close()
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogRemarkVisible = false
              this.getList()
            }
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    // 多选
    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },

    filtercolumn(scope, item) {
      return scope.row[item]
      // if (item === 'status') {
      //   return scope.row[item] === 'ZT01' ? '禁用' : '启用'
      // } else {
      //   return scope.row[item]
      // }
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.userName = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        areaId: this.areaId,
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      if (this.searchshow) {
        query['userName'] = this.listQuery.userName
      } else {
        query['userName'] = this.search.userName
        query['concludeStatus'] = this.search.concludeStatus
        query['certificateStatus'] = this.search.certificateStatus
      }
      const prams = {
        areaId: this.areaId
      }
      return new Promise((resolve, reject) => {
        concludeGetData(prams)
          .then((res) => {
            if (res.data.code === '200') {
              if (res.data.data.type === '1') {
                this.manualStatus = false
              } else {
                this.manualStatus = true
              }
              queryPage(query)
                .then((response) => {
                  this.list = response.data.data.rows
                  this.total = response.data.data.total
                  this.listLoading = false
                })
                .catch((error) => {
                  reject(error)
                  this.listLoading = false
                })
            }
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },
    getScoresList() {
      this.testScoresLoading = true
      var query = {
        areaId: this.areaId,
        page: this.listQuerys.page,
        pageSize: this.listQuerys.limit,
        userId: this.userId
      }
      return new Promise((resolve, reject) => {
        ScoreList(query)
          .then((res) => {
            if (res.data.code === '200') {
              this.ScoresList = res.data.data.rows
              this.totals = res.data.data.total
              this.testScoresLoading = false
            }
          })
          .catch((error) => {
            reject(error)
            this.testScoresLoading = false
          })
      })
    },

    /**
     * 获取字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((res) => {
            const datas = res.data.data && res.data.data.length > 0 ? res.data.data[0].list : []
            this.$set(this.dict, code, datas)
            this.$forceUpdate()
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  }
}
</script>
<style lang="scss">
.class-main {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
}
</style>
